<?php

class GZipPlugin extends ShopalongPlugin
{
	var $_extZlibLoaded;
	var $_iniZlibOutputCompression;
	
	function GZipPlugin()
	{
		global $eventManager;
		$eventManager->addListener('OutputStarting', $this);
		$eventManager->addListener('OutputEnded', $this);
	}
	
	function onOutputStarting($params)
	{
		if ($this->_extZlibLoaded = extension_loaded('zlib') && PHP_VERSION >= '4')
		{
			if (($this->_iniZlibOutputCompression = (int)ini_get('zlib.output_compression')) < 1)
			{
				if (PHP_VERSION >= '4.0.4')
				{
					ob_start('ob_gzhandler');
				}
				
				else
				{
					include(DIR_WS_FUNCTIONS . 'gzip_compression.php');
					ob_start();
					ob_implicit_flush();
				}
			}
			
			else
			{
				ini_set('zlib.output_compression_level', GZIP_LEVEL);
			}
		}
	}
	
	function onOutputEnded($params)
	{
		if ($this->_extZlibLoaded == true && ($this->_iniZlibOutputCompression < 1) )
		{
			if ( (PHP_VERSION < '4.0.4') && (PHP_VERSION >= '4') )
			{
				tep_gzip_output(GZIP_LEVEL);
			}
		}
	}
}

?>
